#!/usr/bin/env python3
"""
演示如何通过 HTTP 访问 MCP Tool ActionRequiredLogger
"""

import json
import requests

def demo_mcp_http_access():
    """演示 MCP Tool 的 HTTP 访问方式"""
    
    print("=== MCP Tool HTTP 访问演示 ===\n")
    
    # 基础 URL
    base_url = "http://localhost:5000"  # 直接使用 EmailQA API
    
    print("方式 1: 直接调用 EmailQA API")
    print("-" * 40)
    
    # 测试数据
    test_data = {
        "email_id": "demo_user@example.com",
        "task_id": "demo_http_test_001",
        "email_info_json": json.dumps({
            "subject": "演示邮件主题",
            "sender": "user@example.com",
            "content": "这是一个演示邮件，展示如何通过 HTTP 访问 MCP 工具。"
        }),
        "sop_json": json.dumps({
            "steps": ["接收邮件", "分析内容", "生成回复", "发送回复"],
            "priority": "normal",
            "estimated_time": "5 minutes"
        }),
        "generated_reply": "感谢您的邮件！我们已经收到您的问题，正在处理中。预计会在24小时内给您回复。",
        "action_type": "reply",
        "created_by": "demo_http_client"
    }
    
    print("请求数据:")
    print(json.dumps(test_data, indent=2, ensure_ascii=False))
    print("\n发送请求...")
    
    try:
        # 发送 POST 请求到 EmailQA API
        response = requests.post(
            f"{base_url}/api/email_pending_task/",
            json=test_data,
            timeout=10
        )
        
        print(f"响应状态码: {response.status_code}")
        
        if response.status_code == 201:
            result = response.json()
            print("SUCCESS: 请求成功!")
            print("响应结果:")
            print(json.dumps(result, indent=2, ensure_ascii=False))
        else:
            print("FAILED: 请求失败!")
            print(f"错误信息: {response.text}")
            
    except Exception as e:
        print(f"ERROR: 请求异常: {e}")
    
    print("\n" + "=" * 50 + "\n")
    
    print("方式 2: 通过 MCP Tool HTTP 服务器 (如果运行在 8080 端口)")
    print("-" * 40)
    
    mcp_base_url = "http://localhost:8080"
    
    # MCP Tool 的请求数据
    mcp_test_data = {
        "email_id": "mcp_demo_user@example.com",
        "email_info_json": json.dumps({
            "subject": "MCP HTTP 演示邮件",
            "sender": "demo@example.com",
            "content": "通过 MCP HTTP 服务器发送的演示邮件。"
        }),
        "sop_json": json.dumps({
            "steps": ["MCP 接收", "工具处理", "API 调用", "结果返回"],
            "priority": "high"
        }),
        "generated_reply": "通过 MCP Tool 处理的回复邮件。",
        "action_type": "reply"
    }
    
    print("MCP 请求数据:")
    print(json.dumps(mcp_test_data, indent=2, ensure_ascii=False))
    
    try:
        # 尝试调用 MCP HTTP 服务器
        response = requests.post(
            f"{mcp_base_url}/api/mcp/action_required_logger",
            json=mcp_test_data,
            timeout=10
        )
        
        print(f"\nMCP 响应状态码: {response.status_code}")
        
        if response.status_code == 200:
            result = response.json()
            print("SUCCESS: MCP 请求成功!")
            print("MCP 响应结果:")
            print(json.dumps(result, indent=2, ensure_ascii=False))
        else:
            print("FAILED: MCP 请求失败!")
            print(f"错误信息: {response.text}")
            print("提示: 请确保 MCP HTTP 服务器正在运行在 8080 端口")
            
    except requests.exceptions.ConnectionError:
        print("ERROR: 无法连接到 MCP HTTP 服务器")
        print("提示: 请运行 'python mcp_http_server.py' 启动服务器")
    except Exception as e:
        print(f"ERROR: MCP 请求异常: {e}")
    
    print("\n" + "=" * 50 + "\n")
    
    print("方式 3: 使用 curl 命令行工具")
    print("-" * 40)
    
    print("# 直接调用 EmailQA API:")
    print('curl -X POST http://localhost:5000/api/email_pending_task/ \\')
    print('  -H "Content-Type: application/json" \\')
    print('  -d \'{"email_id": "test@example.com", "task_id": "curl_test_001", "email_info_json": "{\\"subject\\": \\"Test\\"}", "sop_json": "{\\"steps\\": [\\"step1\\"]}", "generated_reply": "Reply", "action_type": "reply", "created_by": "curl"}\'')
    
    print("\n# 通过 MCP HTTP 服务器:")
    print('curl -X POST http://localhost:8080/api/mcp/action_required_logger \\')
    print('  -H "Content-Type: application/json" \\')
    print('  -d \'{"email_id": "test@example.com", "email_info_json": "{\\"subject\\": \\"Test\\"}", "sop_json": "{\\"steps\\": [\\"step1\\"]}", "generated_reply": "Reply", "action_type": "reply"}\'')

if __name__ == "__main__":
    demo_mcp_http_access()